﻿<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
    private List<string> files = new List<string>();
    protected void Page_Load(object sender, EventArgs e)
    {
        string root=Context.Server.MapPath("~/");
        AddSubFiles(root,root);
        BuildSiteMapFile();     
    }

    private void BuildSiteMapFile()
    {
        StringBuilder xmlBuilder = new StringBuilder();
        xmlBuilder.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
        xmlBuilder.AppendLine("<urlset xmlns=\"http://www.google.com/schemas/sitemap/0.84\">");
        foreach (string file in files)
        {
            FileInfo fileInfo=new FileInfo(Context.Server.MapPath(file));
            xmlBuilder.AppendLine("<url>");
            xmlBuilder.AppendLine("<loc>"+file+"</loc>");
            xmlBuilder.AppendLine("<lastmod>"+fileInfo.LastWriteTime.ToString("yyyy-MM-dd")+"</lastmod>");
            xmlBuilder.AppendLine("<changefreq>weekly</changefreq>");
            xmlBuilder.AppendLine("</url>");
        }
        xmlBuilder.AppendLine("</urlset>");
        XmlDocument xml = new XmlDocument();
        xml.LoadXml(xmlBuilder.ToString());
        string xmlFile = Context.Server.MapPath("~") + "Sitemaps.xml";
        xml.Save(xmlFile);
    }
    
    private void AddSubFiles(string path,string root)
    {
        string[] dirs = Directory.GetDirectories(path);
        for (int i = 0; i < dirs.Length; i++)
            AddSubFiles(dirs[i],root);
        string[] fs = Directory.GetFiles(path,"*.aspx");
        
        foreach (string f in fs)
        {
            FileInfo fi = new FileInfo(f);
            string formatted = f.Replace(root, "~/").Replace("\\", "/");
            files.Add(formatted);
            //Response.Write("<p>"+formatted+"</p>");
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>
